﻿package plugin.instances.game
{
    import com.core.*;
    import com.greensock.*;
    import com.sh.game.global.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import morn.core.managers.*;
    import plugin.instances.*;
    import uiresource.instances.ui.*;
    import view.mornExtends.*;

    public class YhfgGame extends YhfgUI implements IInstance
    {
        private var rankList:Array;
        private var imgBox:Box;
        private var curTime:int = 0;
        private var isLoaded:Boolean = false;
        private var tween:TweenLite;
        private var timelite:TimelineLite;
        private var rec:Rectangle;

        public function YhfgGame()
        {
            this.rankList = [];
            this.rec = new Rectangle(0, -10, 0, 100);
            App.mloader3.loadAssets([{url:Config.getUrl("assets/images/speff/yhfg/1.swf"), type:ResLoader.SWF, size:100, priority:1}, {url:Config.getUrl("assets/images/speff/yhfg/2.swf"), type:ResLoader.SWF, size:100, priority:1}, {url:Config.getUrl("assets/images/speff/yhfg/3.swf"), type:ResLoader.SWF, size:100, priority:1}, {url:Config.getUrl("assets/images/speff/yhfg/4.swf"), type:ResLoader.SWF, size:100, priority:1}, {url:Config.getUrl("assets/images/speff/yhfg/5.swf"), type:ResLoader.SWF, size:100, priority:1}, {url:Config.getUrl("assets/images/speff/yhfg/change.swf"), type:ResLoader.SWF, size:100, priority:1}], new Handler(this.onComplete));
            return;
        }// end function

        private function onComplete() : void
        {
            this.isLoaded = true;
            return;
        }// end function

        public function initShow(param1:Object, param2:Dictionary) : void
        {
            var _loc_3:Object = null;
            txt_killCount.text = "0";
            list_msg.array = [];
            list_msg.renderHandler = new Handler(this.rankRender);
            if (param2[InstancesPublic.YHFGSELFSCORE])
            {
                _loc_3 = param2[InstancesPublic.YHFGSELFSCORE];
                ModelProxy.change("data", "yhfgSelfScore", _loc_3.number);
                txt_killCount.text = _loc_3.number;
            }
            if (param2[InstancesPublic.YHFGRANK])
            {
                _loc_3 = param2[InstancesPublic.YHFGRANK];
                this.rankList = _loc_3.info;
                list_msg.array = this.getTopTen();
                txt_unionscore.text = _loc_3.rank > 0 ? (_loc_3.rank) : ("0");
                list_msg.renderHandler = new Handler(this.rankRender);
            }
            shuoming.toolTip = param1.tips.replace(/#""#/g, "\n");
            this.showItem(param1.show);
            return;
        }// end function

        private function showItem(param1:String) : void
        {
            var _loc_2:* = new ItemSplit();
            var _loc_3:* = _loc_2.doSplits(param1, "&", "/", "#");
            list.array = _loc_3[0];
            list.renderHandler = new Handler(this.itemRender);
            return;
        }// end function

        private function itemRender(param1:Component, param2:int) : void
        {
            if (param2 < list.length)
            {
                (param1 as FlashItem).data = list.array[param2];
            }
            return;
        }// end function

        public function tweenComplete(param1:Image) : void
        {
            param1.url = "png.yhfg." + this.curTime;
            if (this.timelite)
            {
                this.timelite.kill();
                this.timelite = null;
            }
            this.timelite = new TimelineLite();
            this.timelite.append(new TweenLite(param1, 0.1, {scaleX:2, scaleY:2, x:95, y:-12, alpha:1}));
            this.timelite.append(new TweenLite(param1, 0.1, {scaleX:1, scaleY:1, y:10, x:110, onComplete:this.timeliteComplete}));
            return;
        }// end function

        public function timeliteComplete() : void
        {
            if (this.timelite)
            {
                this.timelite.kill();
                this.timelite = null;
            }
            return;
        }// end function

        public function updateScroll() : void
        {
            if (this.imgBox)
            {
                this.rec.width = Number(this.imgBox.dataSource);
                this.imgBox.scrollRect = this.rec;
            }
            return;
        }// end function

        public function updateShow(param1:int, param2:Object) : void
        {
            var _loc_3:Image = null;
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:Boolean = false;
            var _loc_7:Image = null;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:Object = null;
            switch(param1)
            {
                case InstancesPublic.TIMECHECKER:
                {
                    _loc_4 = int(param2);
                    if (this.curTime > 1)
                    {
                        var _loc_11:String = this;
                        var _loc_12:* = this.curTime - 1;
                        _loc_11.curTime = _loc_12;
                        if (this.imgBox)
                        {
                            _loc_3 = this.imgBox.getChildByName("fucktime") as Image;
                            if (_loc_3)
                            {
                                if (this.tween)
                                {
                                    this.tween.kill();
                                    this.tween = null;
                                }
                                this.tween = TweenLite.to(_loc_3, 0.2, {alpha:0, onComplete:this.tweenComplete, onCompleteParams:[_loc_3]});
                            }
                        }
                    }
                    else if (this.imgBox)
                    {
                        GlobalLayer.instance.root.stage.removeChild(this.imgBox);
                        while (this.imgBox.numChildren > 0)
                        {
                            
                            this.imgBox.removeChildAt(0);
                        }
                        this.imgBox = null;
                    }
                    if (_loc_4 > 60)
                    {
                        if ((_loc_4 - 5) % 300 == 0)
                        {
                            this.curTime = 5;
                            if (this.isLoaded == true)
                            {
                                this.imgBox = new Box();
                                _loc_7 = new Image("png.yhfg.change");
                                this.imgBox.addChild(_loc_7);
                                _loc_3 = new Image("png.yhfg.5");
                                _loc_3.name = "fucktime";
                                _loc_3.x = 110;
                                _loc_3.y = 10;
                                this.imgBox.addChild(_loc_3);
                                GlobalLayer.instance.root.stage.addChild(this.imgBox);
                                _loc_8 = GlobalLayer.instance.root.stage.stageWidth - this.imgBox.width >> 1;
                                _loc_9 = GlobalLayer.instance.root.stageHeight - this.imgBox.height >> 1;
                                this.imgBox.x = _loc_8;
                                this.imgBox.y = _loc_9;
                                if (this.timelite)
                                {
                                    this.timelite.kill();
                                    this.timelite = null;
                                }
                                this.rec.width = 0;
                                this.imgBox.scrollRect = this.rec;
                                this.imgBox.dataSource = 0;
                                _loc_3.alpha = 0;
                                this.timelite = new TimelineLite();
                                this.timelite.append(new TweenLite(this.imgBox, 0.2, {onUpdate:this.updateScroll, dataSource:300}));
                                this.timelite.append(new TweenLite(_loc_3, 0.1, {scaleX:2, scaleY:2, x:95, y:-12, alpha:1}));
                                this.timelite.append(new TweenLite(_loc_3, 0.1, {scaleX:1, scaleY:1, y:10, x:110, onComplete:this.timeliteComplete}));
                            }
                        }
                    }
                    txt_time.text = TimeDataFormatUtil.getLeftTime(_loc_4);
                    break;
                }
                case InstancesPublic.YHFGSELFCAMP:
                {
                    break;
                }
                case InstancesPublic.YHFGSELFSCORE:
                {
                    ModelProxy.change("data", "yhfgSelfScore", param2.number);
                    _loc_5 = param2.number - Number(txt_killCount.text);
                    if (_loc_5 > 0)
                    {
                        ModelProxy.change("mainui", "bmRunAdd", {type:2, count:_loc_5});
                    }
                    txt_killCount.text = param2.number;
                    break;
                }
                case InstancesPublic.YHFGRANK:
                {
                    this.rankList = param2.info;
                    list_msg.array = this.getTopTen();
                    txt_unionscore.text = param2.rank > 0 ? (param2.rank) : ("0");
                    list_msg.renderHandler = new Handler(this.rankRender);
                    break;
                }
                case InstancesPublic.YHFGSOMEONEBEKILLED:
                {
                    _loc_6 = false;
                    for each (_loc_10 in this.rankList)
                    {
                        
                        if (_loc_10.rid == param2.rid)
                        {
                            _loc_6 = true;
                            _loc_10.score = param2.score;
                            break;
                        }
                    }
                    if (!_loc_6)
                    {
                        this.rankList.push(param2);
                    }
                    list_msg.array = this.getTopTen();
                    break;
                }
                case InstancesPublic.YHFGRANKCHANGE:
                {
                    txt_unionscore.text = param2 > 0 ? (String(param2)) : ("0");
                    break;
                }
                default:
                {
                    break;
                }
            }
            return;
        }// end function

        public function getTopTen() : Array
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Object = null;
            var _loc_1:Array = [];
            if (this.rankList.length > 1)
            {
                _loc_2 = 0;
                while (_loc_2 < this.rankList.length)
                {
                    
                    if (_loc_2 == 10)
                    {
                        return _loc_1;
                    }
                    _loc_3 = this.rankList.length - 2;
                    while (_loc_3 >= _loc_2)
                    {
                        
                        if (int(this.rankList[_loc_3].score) < int(this.rankList[(_loc_3 + 1)].score))
                        {
                            _loc_4 = this.rankList[_loc_3];
                            this.rankList[_loc_3] = this.rankList[(_loc_3 + 1)];
                            this.rankList[(_loc_3 + 1)] = _loc_4;
                        }
                        _loc_3 = _loc_3 - 1;
                    }
                    _loc_1.push(this.rankList[_loc_2]);
                    _loc_2++;
                }
            }
            else
            {
                return this.rankList;
            }
            return _loc_1;
        }// end function

        private function rankRender(param1:Component, param2:int) : void
        {
            var _loc_3:Object = null;
            var _loc_4:Label = null;
            var _loc_5:Label = null;
            var _loc_6:Label = null;
            if (param2 < list_msg.length)
            {
                _loc_3 = list_msg.array[param2];
                _loc_4 = param1.getChildByName("rankindex") as Label;
                _loc_5 = param1.getChildByName("username") as Label;
                _loc_6 = param1.getChildByName("jifencount") as Label;
                _loc_4.text = String((param2 + 1));
                _loc_5.text = _loc_3.name;
                _loc_6.text = _loc_3.score;
            }
            return;
        }// end function

        public function destroyShow(param1:Dictionary) : void
        {
            if (this.imgBox)
            {
                GlobalLayer.instance.root.stage.removeChild(this.imgBox);
                while (this.imgBox.numChildren > 0)
                {
                    
                    this.imgBox.removeChildAt(0);
                }
                this.imgBox = null;
            }
            if (this.tween)
            {
                this.tween.kill();
                this.tween = null;
            }
            if (this.timelite)
            {
                this.timelite.kill();
                this.timelite = null;
            }
            return;
        }// end function

        public function initEvent() : void
        {
            btn_tuichu.clickHandler = new Handler(this.doTuichu);
            return;
        }// end function

        private function doTuichu() : void
        {
            ModelProxy.change("zone", "exitInstance", null);
            return;
        }// end function

        protected function changeShow(event:MouseEvent) : void
        {
            return;
        }// end function

        public function removeEvent() : void
        {
            return;
        }// end function

    }
}
